const app = getApp();
Page({
  data: {
    carousel: [],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    circular: true,
    isrefresh:true,
    page:0,
    totalpage:0,
    iconsArr: [
      { "img": "../../images/nav_icon_01.png", "name": "推荐" },
      { "img": "../../images/nav_icon_02.png", "name": "美甲" },
      { "img": "../../images/nav_icon_03.png", "name": "美容" },
      { "img": "../../images/nav_icon_04.png", "name": "美发" },
      { "img": "../../images/nav_icon_05.png", "name": "美睫" }
    ],
    goods_info: []

  },
  getCarousel: function () {
    app.http.request(app.globalData.root + app.globalData.carousel, "GET", {}, function (res) {
      // console.log(res.data);
      let imgs_arr = [];
      res.data.forEach(function (e) {
        imgs_arr.push(e);
      });
      this.setData({
        "carousel": imgs_arr
      });
    }.bind(this));
  },
  getProducts: function () {
    app.http.request(app.globalData.root + app.globalData.product, "GET", {"page":this.data.page}, function (res) {
      console.log(res.data); 
      var data = res.data.data;
      let products_arr;
      if(this.data.isrefresh){
        products_arr = [];
         
      }
      else{
        products_arr = this.data.goods_info;
      }
      data.forEach(function (e) {
        products_arr.push(e);
      });
      this.setData({
        "goods_info": products_arr,
        "totalpage":res.data.last_page
      });
    }.bind(this));

  },
  onPullDownRefresh: function () {
    console.log("下拉");
    this.setData({
      "page": 0,
      "isrefresh": true
    });
    this.getProducts();

  },
  onReachBottom: function () {
    console.log("上拉");
    this.setData({
      "page": this.data.page + 1,
      "isrefresh":false
    });
    if(this.data.page>this.data.totalpage){
      return false;
    }
    this.getProducts();
  },
  init: function () {
    //获取轮播图
    this.getCarousel();
    //获取商品
    this.getProducts();
  },
  onLoad: function () {
    //初始化
    this.init();
  },

  toSubscribe: function () {
    console.log(433);
    wx.navigateTo({
      url: '../ye/ye',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  toDetail: function (e) {
    // console.log(e);
    wx.setStorage({
      key: 'list',
      data: this.data.goods_info
    })
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?id='+id,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  }





})